package com.iring.service.common.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iring.dao.common.view.ConsumptionViewDao;
import com.iring.domain.common.view.ConsumptionView;
import com.iring.exception.ParamInvalidException;
import com.iring.service.common.ConsumptionService;
import com.iring.util.common.DateUtil;
import com.iring.vo.common.ConsumpVO;

/**
 * @author wanggan
 */
@Service
public class ConsumptionServiceImpl implements ConsumptionService {

    @Autowired
    ConsumptionViewDao consumptionViewDao;

    private void checkStringParam(String str, String name) {
        if (null == str || str.trim().equals("")) {
            throw new ParamInvalidException(name + " is null");
        }
    }

    /**
     * @see ConsumptionService#listConsumptionDetail(String, ConsumpVO)
     */
    @Override
    public List<ConsumptionView> listConsumptionDetail(String username, ConsumpVO consumpVO) {
        Integer optType = consumpVO.getOpttype();
        if (null == optType) {
            throw new ParamInvalidException("opttype is null");
        }
        String start = consumpVO.getStarttime();
        checkStringParam(start, "starttime");
        String end = consumpVO.getEndtime();
        checkStringParam(end, "endtime");
        if (DateUtil.isStartAfterEnd(start, end)) {
            throw new ParamInvalidException("startTime is after than endTime");
        }
        Date startTime = DateUtil.parseToDate(DateUtil.addStartTimeSub(start));

        Date endTime = DateUtil.parseToDate(DateUtil.addEndTimeSub(end));
        return consumptionViewDao.findByUsernameAndOpttypeAndOpttimeBetween(username, optType,
                startTime, endTime);
    }

}
